//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.entity.Account;
import com.example.entity.Employee;
import com.example.exception.CustomException;
import com.example.mapper.EmployeeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;

    public EmployeeService() {
    }

    public void add(Employee employee) {
        String username = employee.getUsername();
        Employee dbemployee = this.employeeMapper.selectByUsername(username);
        if (dbemployee != null) {
            throw new CustomException("500", "用户名已存在，请更换其他用户名");
        } else {
            String no = employee.getNo();
            Employee dbemployee1 = this.employeeMapper.selectByNo(no);
            if (dbemployee1 != null) {
                throw new CustomException("500", "员工编号已存在，请检查输入");
            } else {
                if (StrUtil.isBlank(employee.getPassword())) {
                    employee.setPassword("123"); // 默认密码 123
                }

                if (StrUtil.isBlank(employee.getName())) {
                    employee.setName(employee.getUsername());
                }

                employee.setRole("EMP");  //必须设置role
                this.employeeMapper.insert(employee);
            }
        }
    }

    public void updateById(Employee employee) {
        this.employeeMapper.updateById(employee);
    }

    public void deleteById(Integer id) {
        this.employeeMapper.deleteById(id);
    }

    public void deleteBatch(List<Integer> ids) {
        Iterator var2 = ids.iterator();

        while(var2.hasNext()) {
            Integer id = (Integer)var2.next();
            this.deleteById(id);
        }

    }

    public Employee selectById(Integer id) {
        return this.employeeMapper.selectById(id);
    }

    public List<Employee> selectAll(Employee employee) {
        return this.employeeMapper.selectAll(employee);
    }

    public List<Employee> selectList(Employee employee) {
        System.out.println(employee);
        return null;
    }

    public PageInfo<Employee> selectPage(Employee employee, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Employee> list = this.employeeMapper.selectAll(employee);
        return PageInfo.of(list);
    }

    public Employee login(Account account) {
        String username = account.getUsername();
        Employee dbemployee = this.employeeMapper.selectByUsername(username);
        if (dbemployee == null) {
            throw new CustomException("500", "账号不存在，请检查后重试");
        } else {
            String password = account.getPassword();
            if (!dbemployee.getPassword().equals(password)) {
                throw new CustomException("500", "密码错误，请重新输入");
            } else {
                return dbemployee;
            }
        }
    }

    public void register(Employee employee) {
        this.add(employee);
    }

    public void updatePassword(Account account) {
        Integer id = account.getId();
        Employee dbemployee = this.selectById(id);
        if (!dbemployee.getPassword().equals(account.getPassword())) {  //最不可能错的放在对比的左侧。如果数据库中的密码和提交表单的密码不一致
            throw new CustomException("500", "原密码错误");
        } else {
            dbemployee.setPassword(account.getNewPassword());
            this.updateById(dbemployee);
        }
    }
}
